# Copyright (c) 2012-2016 Arxopia LLC.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the Arxopia LLC nor the names of its contributors
#     	may be used to endorse or promote products derived from this software
#     	without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL ARXOPIA LLC BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
# OF THE POSSIBILITY OF SUCH DAMAGE.

module Risu
	module Templates
		module MalwareTemplateHelper

			#
			def conficker_count
				begin
					return Item.where(:plugin_id => Plugin.where(:plugin_name => "Conficker Worm Detection (uncredentialed check)").first.id).count
				rescue => e
					return 0
				end
			end

			#
			def conficker_appendix_section
				if conficker_count() <= 0
					return
				end

				heading2 "Conficker Worm Infection"

				headers = ["Host"]
				data = Array.new

				findings =  Item.where(:plugin_id => Plugin.where(:plugin_name => "Conficker Worm Detection (uncredentialed check)").first.id)

				findings.each do |finding|
					host = Host.find_by_id(finding.host_id)

					host_string = "#{host.name}"
					host_string << " (#{host.fqdn})" if host.fqdn != nil

					row = Array.new
					row.push host_string

					data << row
				end

				@output.table([headers] + data, :header => true, :width => output.bounds.width) do
					row(0).style(:font_style => :bold, :background_color => 'cccccc')
					cells.borders = [:top, :bottom, :left, :right]
				end

				text "\n"
			end

			#
			def conficker_section
				if conficker_count() <= 0
					return
				end

				conficker_count = Item.where(:plugin_id => Plugin.where(:plugin_name => "Conficker Worm Detection (uncredentialed check)").first.id).count
				heading2 "Conficker Worm Infection"

				text "Conficker Worm infections were found on #{conficker_count} of #{Report.title}'s computer systems. Conficker, also known as Downup, Downadup and Kido, is a computer worm targeting the Microsoft Windows operating system that was first detected in November 2008. It uses flaws in Windows software and dictionary attacks on administrator passwords to propagate while forming a botnet, and has been unusually difficult to counter because of its combined use of many advanced malware techniques. The systems of interest are detailed in the detailed findings report with remediation steps."

				text "\n"
			end

			#
			def known_malicious_process_count
				begin
					return Item.where(:plugin_id => 59275).count
				rescue => e
					return 0
				end
			end

			#
			def known_malicious_process_section
				count = known_malicious_process_count()

				if count <= 0
					return
				end

				heading1 "Known Malicious Process Detected" if count == 1
				heading1 "Known Malicious Processes Detected" if count > 1

				text "A known malicious process was detected active on the network. This process was detected using hash binary hashing. This hash was submitted to an malware detection service that checks each hash against several different anti virus software suites. Details can be found in Appendix A."

				text "\n"
			end

			#
			def known_malicious_process_appendix_section
				count = known_malicious_process_count()

				if count <= 0
					return
				end

				heading2 "Known Malicious Process" if count == 1
				heading2 "Known Malicious Processes" if count > 1

				findings = Item.where(:plugin_id => 59275)
				plugin = Plugin.find_by_id(59275)

				findings.each do |finding|
					host = Host.find_by_id(finding.host_id)

					text "Host", :style => :bold
					host_string = "#{host.name}"
					host_string << " (#{host.fqdn})" if host.fqdn != nil
					text host_string

					definition "Description", plugin.description.gsub(/[ ]{2,}/, " ") if plugin.description != nil
					definition "Plugin output", finding.plugin_output.gsub(/Any detected files 5 MB or less are available as attachments./, "")
				end

				text "\n"
			end

			#
			def malware_section
				conficker_section
				known_malicious_process_section
			end

			#
			def malware_appendix_section
				conficker_appendix_section
				known_malicious_process_appendix_section
			end
		end
	end
end
